package com.cr.admin.system.controller;

import com.cr.admin.system.pojo.dto.SysMenuAddParam;
import com.cr.admin.system.pojo.dto.SysMenuUpdateParam;
import com.cr.admin.system.pojo.entity.SysMenu;
import com.cr.admin.system.pojo.vo.SysMenuTreeVO;
import com.cr.admin.system.service.ISysMenuService;
import com.cr.common.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "50.菜单管理")
@RestController
@RequestMapping("/api/sys/menus")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @ApiOperation(value = "新增菜单")
    @ApiOperationSupport(order = 100)
    @PostMapping
    public JsonResult add(@Validated @RequestBody SysMenuAddParam sysMenuAddParam) {
        sysMenuService.insert(sysMenuAddParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "删除菜单")
    @ApiOperationSupport(order = 200)
    @DeleteMapping
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        sysMenuService.deleteByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value = "修改菜单")
    @ApiOperationSupport(order = 300)
    @PutMapping
    public JsonResult update(@Validated @RequestBody SysMenuUpdateParam sysMenuUpdateParam) {
        sysMenuService.updateById(sysMenuUpdateParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询菜单详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        SysMenu sysMenu = sysMenuService.selectById(id);
        return JsonResult.success(sysMenu);
    }

    @ApiOperation(value = "查询菜单列表")
    @ApiOperationSupport(order = 410)
    @GetMapping
    public JsonResult list() {
        List<SysMenuTreeVO> list = sysMenuService.selectSysMenuTreeVOList();
        return JsonResult.success(list);
    }

}
